import os
import sys
import requests
import json
import re
from lxml import etree

cwd = os.getcwd()
sys.path.append(cwd)
print(cwd)

from pub_func import DownloadFile, folder_exist, file_write

base_url = 'https://lovepik.com/photo-149292-0-{}-0-0-0.html'

headers = {
    'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36',
    'Referer':'https://zh.lovepik.com/images/ice-formation.html', # 需要搜索的关键字rime
}

if __name__ == '__main__':
    for i in range(1, 20):
        response = requests.get(url=base_url.format(i), headers=headers)
        response.encoding = 'utf8'
        # print(response.text)
        html = etree.HTML(response.text)
        list_img_url = html.xpath('//*[@id="main_lists"]/div/div[2]/div/a/img/@data-original')
        print(list_img_url, len(list_img_url))
        path = os.path.join('/home/ubuntu/workspace/img' ,'lovepik_8.csv')
        for url_img in list_img_url[:-1]:
            content = url_img + ',\n'
            file_write(path, content, 'a')
